<template>
    <div>
      <!--面包屑导航区域-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>参数列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!--卡片区域-->
      <el-card>
        <el-alert type="warning" title="注意：只允许为第三级分类设置相关参数" :closable="false" show-icon></el-alert>
        <!--选择商品分类区域-->
        <el-row class="cat_opt">
          <el-col>
            <span>选择商品分类:</span>
            <!--选择商品分类的级联选择框-->
            <el-cascader expand-trigger="hover"  clearable
                         v-model="selectedopt"
                         :options="cateList"
                         :props="cascadeProps" @change="handleChange"
            >
            </el-cascader>
          </el-col>
        </el-row>
        <!--tab页签区域-->
        <el-tabs v-model="activeName" @tab-click="handleTabClick">
          <el-tab-pane label="动态参数" name="many">
            <el-button type="primary" size="mini" :disabled="selectedopt.length<3?true:false" @click="addManyClick">添加参数</el-button>
            <!--动态参数表格-->
            <el-table :data="manyData" border stripe>
              <el-table-column type="expand">
                <template slot-scope="scope">
                  <el-tag v-for="(item,index) in scope.row.attr_vals"  :key="index" size="mini" closable>{{item}}</el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="scope.row.inputVisible"
                    v-model="scope.row.inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  >
                  </el-input>
                  <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                </template>
              </el-table-column> //展开行
              <el-table-column type="index"></el-table-column>
              <el-table-column label='参数名称' prop="attr_name"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                  <el-button type="danger" icon="el-icon-delete" size="mini">>编辑</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="静态属性" name="only">
            <el-button type="primary" size="mini" :disabled="selectedopt.length<3?true:false" @click="addManyClick">添加属性</el-button>
            <!--静态属性表格-->
            <el-table :data="onlyData" border stripe>
              <el-table-column type="expand"></el-table-column> //展开行
              <el-table-column type="index"></el-table-column>
              <el-table-column label='属性名称' prop="attr_name"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                  <el-button type="danger" icon="el-icon-delete" size="mini">>编辑</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!--添加参数的对话框-->
      <el-dialog
        title="提示"
        :visible.sync="addParamsDialogVisible"
        width="50%"
       >
        参数名称:<el-input v-model="inputparam" placeholder="请输入内容" ref="inputparamref"></el-input>
        <span slot="footer" class="dialog-footer">
    <el-button @click="addParamsDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addparams">确 定</el-button>
  </span>
      </el-dialog>
    </div>
</template>

<script>
  export default {
    name: 'Params',
    data:function () {
      return {
          selectedopt:[],
          cateList:[],
        cascadeProps:{
          value:'cat_id',
          label:'cat_name',
          children:'children'
        },
        activeName: 'many',
        //动态参数数据
        manyData:[],
        //静态属性的数据
        onlyData:[],
        //添加参数对话框是否显示
        addParamsDialogVisible: false,
        //添加的参数
        inputparam:'',
        inputVisible: false,
        inputValue: ''
      }
    },
    created () {
      this.getCateList();
    },
    //计算属性
    computed:{
      //当前选中的三级分类的id
      cateId(){
        if(this.selectedopt.length ==3){
          return this.selectedopt[2];
        }
        return null;
      }
    },
    methods:{
      async getCateList(){
        var data=await this.$axios.get('categories');
        var res=data.data;
        if(res.meta.status!=200){return this.$message.error("获取分类数据失败！")}
        this.cateList=res.data;
        console.log(this.cateList)
      },
      getParamsList: async function () {
        if (this.selectedopt.length < 3) {
          this.selectedopt = []
          return
        }
        //选中三级分类，返回所有的属性和参数
        var data = await this.$axios.get('categories/' + this.cateId + '/attributes', { params: { sel: this.activeName } })
        var res = data.data
        if (res.meta.status != 200) {
          return this.$message.error('获取参数列表失败')}
        res.data.forEach(item=>{
          if(item.attr_vals===''){
            item.attr_vals=[];
          }else{
            item.attr_vals=item.attr_vals.split(' ');
          }
          //控制文本框的显示和隐藏
          item.inputVisible=false;
          //文本框中输入的值
          item.inputValue='';

        });
        console.log(res.data)
        if(this.activeName==='many'){
          this.manyData=res.data;
        }else if(this.activeName==='only'){
          this.onlyData=res.data;
        }


      },
      handleChange(){
        if (this.selectedopt.length!==3){
          this.selectedopt=[];
          this.manyData=[];
          this.onlyData=[];
          return;
        }
        this.getParamsList()
      },
      //tab页签点击处理
      handleTabClick(){
        this.getParamsList();
      },
      //添加参数点击事件
      addManyClick(){
        this.addParamsDialogVisible=true;
      },
      //添加参数弹出窗口确定按键
      async addparams(){
       var data=await this.$axios.post("categories/"+this.cateId+'/attributes',{attr_name: this.inputparam,attr_sel:this.activeName});
      var res=data.data;
        if(res.meta.status!=201){return this.$message.error('添加参数失败')}
      this.$message.success('添加参数成功!');
      this.getParamsList();
      this.addParamsDialogVisible=false;
      },
      handleInputConfirm(row){
        row.inputVisible=false;
        row.attr_vals.push(row.inputValue.trim());
        row.inputValue='';
      },
      showInput(item){
        item.inputVisible=true;
        //让文本框获得焦点
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      }
    }
  }
</script>

<style scoped lang="less">
.cat_opt{
  margin-top: 10px;
}
  .el-cascader{
    margin-left: 15px;
    width: 250px;
  }
  .el-tabs{
    margin-top: 15px;
  }
  .el-button{
    margin-bottom: 15px;
  }
  .el-tag{
    margin: 10px;
  }
  .input-new-tag{
    width: 150px;
  }
</style>
